package com.wudizaba.microservicesdemoauth.ExceptionHabdler;

import com.wudizaba.microservicesdemocommon.Exceptions.*;
import com.wudizaba.microservicesdemocommon.R.AxiosResult;
import com.wudizaba.microservicesdemocommon.R.Enum.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 全局异常处理器
 *
 * @author wudizaba
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    private static final String URI = "请求发生异常：{}";

    /**
     * 登录异常
     */
    @ExceptionHandler(value = {LoginException.class})
    public AxiosResult handleException(LoginException e) {
        log.error(URI, e.getMessage());
        return AxiosResult.error(ResponseCode.LOGIN_ERROR.getCode(), e.getMessage());
    }

    /**
     * 账户异常
     */
    @ExceptionHandler(value = {AccountException.class})
    public AxiosResult handleException(AccountException e) {
        log.error(URI, e.getMessage());
        return AxiosResult.error(ResponseCode.USERNAME_OR_PASSWORD_ERROR.getCode(), e.getMessage());
    }


    /**
     * Excel异常
     */
    @ExceptionHandler(value = {ExcelException.class})
    public AxiosResult ExcelException(Exception e) {
        log.error(URI, e.getMessage());
        return AxiosResult.error(ResponseCode.EXCEL_ERROR.getCode(), e.getMessage());
    }

    /**
     * mqtt异常
     */
    @ExceptionHandler(value = {MqttException.class})
    public AxiosResult MqttException(Exception e) {
        log.error(URI, e.getMessage());
        return AxiosResult.error(ResponseCode.MQTT_SERVER_ERROR.getCode(), e.getMessage());
    }

    /**
     * quartz异常
     */
    @ExceptionHandler(value = {QuartzException.class})
    public AxiosResult QuartzException(Exception e) {
        log.error(URI, e.getMessage());
        return AxiosResult.error(ResponseCode.QUARTZ_SERVER_ERROR.getCode(), e.getMessage());
    }
}
